{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典简介\n",
    "在该 notebook 中，你将要学习 Python 字典。\n",
    "\n",
    "## 字典将 `keys` 与 `values` 相关联"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "ticket = {\n",
    "    \"date\" : \"2018-12-28\",\n",
    "    \"priority\" : \"high\",\n",
    "    \"description\" : \"\"\"Vehicle did not slow down despite\n",
    "    SLOW    \n",
    "    SCHOOL    \n",
    "    ZONE    \"\"\"\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你在上面看到的`ticket` 是一个Python`dictionary`。 字典是一种 **无序的** 数据结构。\n",
    "\n",
    "使用`list`可以根据 **位置**（例如`my_list[0]`）访问数据，但字典是无序的。 字典中的数据需要通过它的关键字而不是它的位置来访问。\n",
    "\n",
    "在上面的`ticket`示例中，有三个**关键字**。 你可以通过运行下面的代码来看看它们分别是什么。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['date', 'priority', 'description'])\n"
     ]
    }
   ],
   "source": [
    "print(ticket.keys())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "字典中的每个**关键字**都与一个**值**相关联。 下面的代码可以检索与**关键字**描述关联的一个**值**。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vehicle did not slow down despite\n",
      "    SLOW    \n",
      "    SCHOOL    \n",
      "    ZONE    \n"
     ]
    }
   ],
   "source": [
    "print(ticket['description'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字典是*可变的*（可以被修改） \n",
    "\n",
    "字典是**可变的**，这意味着它们可以被改变（修改）。“可变的\"意味着：\n",
    "\n",
    "1. 可以在词典中**添加**元素。\n",
    "2. 可以在词典中**删除**元素。\n",
    "3. 可以在词典中**修改**元素。\n",
    "\n",
    "下面的代码演示了如何添加、删除与修改词典中的元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "# Let's start with an empty dictionary. Eventually this will store \n",
    "# English to spanish translations...\n",
    "\n",
    "eng_to_spa = {} # this creates an empty dictionary\n",
    "print(eng_to_spa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'blue': 'Azul', 'gren': 'verde'}\n"
     ]
    }
   ],
   "source": [
    "# 1. Adding elements to a dictionary.\n",
    "#\n",
    "#    Elements can be added to a dictionary as follows:\n",
    "\n",
    "eng_to_spa['blue']  = 'Azul'\n",
    "eng_to_spa['gren']  = 'verde'\n",
    "\n",
    "print(eng_to_spa)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "哎呀！ 我把“绿色”两个字打错了。 让我们从该词典中把该元素删除吧..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'blue': 'Azul'}\n"
     ]
    }
   ],
   "source": [
    "# 2. Removing elements from a dictionary\n",
    "#    \n",
    "#    Elements can be removed from a dictionary using the del keyword\n",
    "\n",
    "del eng_to_spa['gren']\n",
    "\n",
    "print(eng_to_spa)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "它看起来像“azul”，是大写的...让我们把它修改一下吧！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'blue': 'azul'}\n"
     ]
    }
   ],
   "source": [
    "# 3. Modifying elements in a dictionary.\n",
    "#\n",
    "#    Modifying the value associated with a key works just\n",
    "#    like adding a new value...\n",
    "\n",
    "eng_to_spa['blue'] = 'azul'\n",
    "\n",
    "print(eng_to_spa)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### TODO - 完成 `eng_to_spa` 函数 \n",
    "\n",
    "在下面的代码单元格中，添加一些额外的颜色，使`eng_to_spa` 能够反映出如下对应关系：\n",
    "\n",
    "| 英语     |  西班牙语   |\n",
    "| ------ | :-----: |\n",
    "| blue   |  azul   |\n",
    "| green  |  verde  |\n",
    "| pink   |  rosa   |\n",
    "| orange | naranja |\n",
    "| gray   |  gris   |\n",
    "| brown  | marron  |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your english to spanish dictionary is looking good!\n"
     ]
    }
   ],
   "source": [
    "eng_to_spa['blue'] = 'azul'\n",
    "eng_to_spa['green'] = 'verde'\n",
    "eng_to_spa['pink'] = 'rosa'\n",
    "eng_to_spa['orange'] = 'naranja'\n",
    "eng_to_spa['gray'] = 'gris'\n",
    "eng_to_spa['brown'] = 'marron'\n",
    "\n",
    "# YOUR CODE HERE - complete the eng_to_spa\n",
    "#   dictionary so it contains all the information\n",
    "#   shown in the table above.\n",
    "\n",
    "# Testing code below\n",
    "assert(eng_to_spa['blue'] == 'azul')\n",
    "assert(eng_to_spa['gray'] == 'gris')\n",
    "assert(eng_to_spa['orange'] == 'naranja')\n",
    "assert(eng_to_spa['pink'] == 'rosa')\n",
    "print(\"Your english to spanish dictionary is looking good!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字典是*无序的*\n",
    "\n",
    "字典是**无序的**，具体指的是什么意思呢？ 看一看以下两个票（ ticket）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Does dictionary_ticket_1 == dictionary_ticket_2?\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "dictionary_ticket_1 = {\n",
    "    \"date\"       : \"2018-12-31\",\n",
    "    \"priority\"   : \"high\",\n",
    "    \"description\": \"Vehicle made unexpected stop.\"\n",
    "}\n",
    "\n",
    "dictionary_ticket_2 = {\n",
    "    \"priority\"   : \"high\",\n",
    "    \"description\": \"Vehicle made unexpected stop.\",\n",
    "    \"date\"       : \"2018-12-31\"\n",
    "}\n",
    "\n",
    "# these dictionaries contain the same information, but the\n",
    "# ordering looks different. Does Python consider them identical?\n",
    "\n",
    "print(\"Does dictionary_ticket_1 == dictionary_ticket_2?\")\n",
    "print(dictionary_ticket_1 == dictionary_ticket_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 遍历一个字典\n",
    "尽管没有定义明确的元素排序，但你仍然可以遍历字典的**关键字**。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "date\n",
      "priority\n",
      "description\n"
     ]
    }
   ],
   "source": [
    "# demonstration of dictionary looping \n",
    "# demo 1 - keys only\n",
    "\n",
    "for key in dictionary_ticket_1:\n",
    "    print(key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "date : 2018-12-31\n",
      "priority : high\n",
      "description : Vehicle made unexpected stop.\n"
     ]
    }
   ],
   "source": [
    "# demonstration of dictionary looping \n",
    "# demo 2 - keys and values\n",
    "\n",
    "for key in dictionary_ticket_1:\n",
    "    value = dictionary_ticket_1[key]\n",
    "    print(key, ':', value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TODO -   练习：“调换”字典\n",
    "下面的代码重新定义了`eng_to_spa`字典。 你需要做的是编写一个名为`reverse_dictionary`的函数，该函数可以根据一个英西词典，返回一个西英词典。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nice work! Your reverse_dictionary function is correct.\n"
     ]
    }
   ],
   "source": [
    "from Intro_to_dictionaries_zh import reverse_dictionary\n",
    "eng_to_spa = {\n",
    "    \"red\" :    \"rojo\",\n",
    "    \"blue\"   : \"azul\",\n",
    "    \"green\"  : \"verde\",\n",
    "    \"black\"  : \"negro\",\n",
    "    \"white\"  : \"blanco\",\n",
    "    \"yellow\" : \"amarillo\",\n",
    "    \"orange\" : \"naranja\",\n",
    "    \"pink\"   : \"rosa\",\n",
    "    \"purple\" : \"morado\",\n",
    "    \"gray\"   : \"gris\"\n",
    "}\n",
    "\n",
    "# TESTING CODE\n",
    "\n",
    "spa_to_eng = reverse_dictionary(eng_to_spa)\n",
    "\n",
    "assert(len(spa_to_eng) == len(eng_to_spa))\n",
    "assert(spa_to_eng['rojo'] == 'red')\n",
    "assert(spa_to_eng['azul'] == 'blue')\n",
    "assert(spa_to_eng['verde'] == 'green')\n",
    "\n",
    "print(\"Nice work! Your reverse_dictionary function is correct.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 西法词典（西班牙语到法语）\n",
    "\n",
    "在这个练习中，你将要根据两本词典（一个西英词典和一个法英词典），将它们合并成一个西法词典。\n",
    "\n",
    "首先，我们来看看现有的两个字典。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "there are     10 colors in eng_to_spa\n",
      "but there are 11 colors in french_to_eng\n"
     ]
    }
   ],
   "source": [
    "eng_to_spa = {\n",
    "    \"red\" :    \"rojo\",\n",
    "    \"blue\"   : \"azul\",\n",
    "    \"green\"  : \"verde\",\n",
    "    \"black\"  : \"negro\",\n",
    "    \"white\"  : \"blanco\",\n",
    "    \"yellow\" : \"amarillo\",\n",
    "    \"orange\" : \"naranja\",\n",
    "    \"pink\"   : \"rosa\",\n",
    "    \"purple\" : \"morado\",\n",
    "    \"gray\"   : \"gris\"\n",
    "}\n",
    "\n",
    "french_to_eng = {\n",
    "    \"bleu\"  : \"blue\",\n",
    "    \"noir\"  : \"black\", \n",
    "    \"vert\"  : \"green\",\n",
    "    \"violet\": \"purple\",\n",
    "    \"gris\"  : \"gray\",\n",
    "    \"rouge\" : \"red\",\n",
    "    \"orange\": \"orange\",\n",
    "    \"rose\"  : \"pink\",\n",
    "    \"marron\": \"brown\",\n",
    "    \"jaune\" : \"yellow\",\n",
    "    \"blanc\" : \"white\",\n",
    "}\n",
    "\n",
    "print(\"there are    \", len(eng_to_spa), \"colors in eng_to_spa\")\n",
    "print(\"but there are\", len(french_to_eng), \"colors in french_to_eng\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The french word for blue is bleu\n",
      "The french word for black is noir\n",
      "The french word for green is vert\n",
      "The french word for purple is violet\n",
      "The french word for gray is gris\n",
      "The french word for red is rouge\n",
      "The french word for orange is orange\n",
      "The french word for pink is rose\n",
      "The french word for brown is marron\n",
      "The french word for yellow is jaune\n",
      "The french word for white is blanc\n"
     ]
    }
   ],
   "source": [
    "# don't forget you have the \"reverse_dictionary\" function!\n",
    "\n",
    "english_to_french = reverse_dictionary(french_to_eng)\n",
    "\n",
    "for english_word in english_to_french:\n",
    "    french_word = english_to_french[english_word]\n",
    "    print(\"The french word for\", english_word, \"is\", french_word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nice work! Your spanish to french function works correctly!\n"
     ]
    }
   ],
   "source": [
    "from Intro_to_dictionaries_zh import spanish_to_french\n",
    "# TESTING CODE \n",
    "S2F = spanish_to_french(eng_to_spa, english_to_french)\n",
    "\n",
    "assert(S2F[\"rojo\"] == \"rouge\")\n",
    "assert(S2F[\"morado\"] == \"violet\")\n",
    "\n",
    "print(\"Nice work! Your spanish to french function works correctly!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 我的解决方案 \n",
    "\n",
    "在进行下一步之前，请确保你已尝试解决上述问题！\n",
    "\n",
    "当测试我的第一个解决方案时，出现了一个错误。 在下面，尝试运行我的第一次尝试吧，看看它是什么。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'brown'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-16-37fb66b1d546>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      7\u001b[0m     \u001b[0;32mreturn\u001b[0m \u001b[0ms2f\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mS2F_1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mspanish_to_french_1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meng_to_spa\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0menglish_to_french\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m<ipython-input-16-37fb66b1d546>\u001b[0m in \u001b[0;36mspanish_to_french_1\u001b[0;34m(english_to_spanish, english_to_french)\u001b[0m\n\u001b[1;32m      3\u001b[0m     \u001b[0;32mfor\u001b[0m \u001b[0menglish_word\u001b[0m \u001b[0;32min\u001b[0m \u001b[0menglish_to_french\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m         \u001b[0mfrench_word\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0menglish_to_french\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0menglish_word\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m         \u001b[0mspanish_word\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0menglish_to_spanish\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0menglish_word\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      6\u001b[0m         \u001b[0ms2f\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mspanish_word\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfrench_word\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m     \u001b[0;32mreturn\u001b[0m \u001b[0ms2f\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyError\u001b[0m: 'brown'"
     ]
    }
   ],
   "source": [
    "def spanish_to_french_1(english_to_spanish, english_to_french):\n",
    "    s2f = {}\n",
    "    for english_word in english_to_french:\n",
    "        french_word = english_to_french[english_word]\n",
    "        spanish_word = english_to_spanish[english_word]\n",
    "        s2f[spanish_word] = french_word\n",
    "    return s2f\n",
    "\n",
    "S2F_1 = spanish_to_french_1(eng_to_spa, english_to_french)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我不断收到以下消息："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "KeyError: 'brown'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这是因为\"brown\"不在我的西英词典中！\n",
    "\n",
    "没关系，我可以对每个密钥进行**归属检查**。 将上面的代码与下面的代码进行比较。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def spanish_to_french_2(english_to_spanish, english_to_french):\n",
    "    s2f = {}\n",
    "    for english_word in english_to_french:\n",
    "        if english_word in english_to_spanish:\n",
    "            french_word = english_to_french[english_word]\n",
    "            spanish_word = english_to_spanish[english_word]\n",
    "            s2f[spanish_word] = french_word\n",
    "    return s2f\n",
    "\n",
    "S2F_2 = spanish_to_french_2(eng_to_spa, english_to_french)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 对关键字`in`的注释（以及“归属测试”）\n",
    "\n",
    "注意上面的代码中的if语句。当我们编写如下语句时"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-18-dec2e3281a63>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-18-dec2e3281a63>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    if X in Y\u001b[0m\n\u001b[0m             ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "if X in Y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们正在进行“归属测试”。 当`Y`是词典时，我们正在测试`X`是否属于该词典中的**关键字**。 如果是，则测试返回`True`； 否则它返回`False`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Is 'china' a key in this dictionary?\n",
      "True\n",
      "\n",
      "Is 'paris' a key in this dictionary?\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "capitals = {\n",
    "    \"spain\" : \"madrid\",\n",
    "    \"france\" : \"paris\",\n",
    "    \"china\" : \"beijing\"\n",
    "}\n",
    "\n",
    "print(\"Is 'china' a key in this dictionary?\")\n",
    "print(\"china\" in capitals)\n",
    "print()\n",
    "\n",
    "print(\"Is 'paris' a key in this dictionary?\")\n",
    "print(\"paris\" in capitals)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字典小结\n",
    "下面是关于字典的几个重要事项，请牢记！\n",
    "\n",
    "#### 1. 字典将 `keys` 与 `values` 相关联\n",
    "下面的字典有2个关键字（`\"abc\"`和`\"def\"`）和2个值（`123`和`456`）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = {\"abc\":123, \"def\":456}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 字典是无序的\n",
    "如果两个字典具有相同的关键字**并且**这些关键字与相同的值相关联，则这两个字典是相同的。顺序无关紧要。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-21-97bdb6285514>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-21-97bdb6285514>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    > d1 = {\"abc\":123, \"def\":456}\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "> d1 = {\"abc\":123, \"def\":456}\n",
    "> d2 = {\"def\":456, \"abc\":123}\n",
    "> d1 == d2\n",
    "True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. 字典是“可变的”\n",
    "这意味着字典可以通过各种方式进行修改\n",
    "\n",
    "##### 3.1 增加新的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-22-8e642ec79ac0>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-22-8e642ec79ac0>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    > d = {}\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "> d = {}\n",
    "> d['k'] = 'v'\n",
    "> print(d)\n",
    "{'k': 'v'}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 3.2 删除元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "> del d['k']\n",
    "> print(d)\n",
    "{}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 3.3 改变元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "> d['key'] = 'value' # first need to add an element back in\n",
    "> print(d)\n",
    "{'key': 'value'}\n",
    "\n",
    "> d['key'] = 'other value'\n",
    "> print(d)\n",
    "{'key' : 'other value'}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4. 遍历一个字典\n",
    "遍历一个字典时，事实上，你是在循环访问该词典的**关键字**。\n",
    "\n",
    "#### 5. 归属测试\n",
    "\n",
    "Python中的`in`关键字可用于测试某个词是否属于字典中的一个**关键字**。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
